﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	public partial class FormShortcutsSetting : System.Windows.Forms.Form
	{
		public FormShortcutsSetting()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			base.CenterToScreen();
		}

		public static string GetAppRootPath()
		{
			string installLocation = Product.InstallLocation;
			int length = installLocation.LastIndexOf("\\");
			string text = installLocation.Substring(0, length);
			length = text.LastIndexOf("\\");
			return text.Substring(0, length);
		}

		public List<string> GetXmlPaths()
		{
			List<string> list = new List<string>();
			if (Product.IsArchitecture() || Product.IsFamliyLeader())
			{
				string item = Product.UserDataLocation + "\\HYShortcutsArch.xml";
				list.Add(item);
			}
			else if (Product.IsTunnel())
			{
				string item2 = Product.UserDataLocation + "\\HYShortcutsTunnel.xml";
				list.Add(item2);
			}
			else if (Product.IsEDD())
			{
				string item3 = Product.UserDataLocation + "\\HYShortcutsHS.xml";
				list.Add(item3);
			}
			else
			{
				if (Product.HasDiscipline(this.m_DisciplineValue, Product.DisciplineType.HVAC))
				{
					string item4 = Product.UserDataLocation + "\\HYShortcutsHVAC.xml";
					list.Add(item4);
				}
				if (Product.HasDiscipline(this.m_DisciplineValue, Product.DisciplineType.Drainage))
				{
					string item5 = Product.UserDataLocation + "\\HYShortcutsGPS.xml";
					list.Add(item5);
				}
				if (Product.HasDiscipline(this.m_DisciplineValue, Product.DisciplineType.Electric))
				{
					string item6 = Product.UserDataLocation + "\\HYShortcutsElectric.xml";
					list.Add(item6);
				}
			}
			return list;
		}

		public List<string> GetAppMenuList()
		{
			List<string> list = new List<string>();
			if (Product.IsArchitecture() || Product.IsFamliyLeader())
			{
				list.AddRange(this.GetArchRibbonList());
			}
			else if (Product.IsTunnel())
			{
				list.AddRange(this.GetTunnelRibbonList());
			}
			else if (Product.IsEDD())
			{
				list.AddRange(this.GetEDDRibbonList());
			}
			else
			{
				if (Product.HasDiscipline(this.m_DisciplineValue, Product.DisciplineType.HVAC))
				{
					list.AddRange(this.GetHVACRibbonList());
				}
				if (Product.HasDiscipline(this.m_DisciplineValue, Product.DisciplineType.Drainage))
				{
					list.AddRange(this.GetDrainageList());
				}
				if (Product.HasDiscipline(this.m_DisciplineValue, Product.DisciplineType.Electric))
				{
					list.AddRange(this.GetElecticRibbonList());
				}
			}
			if (list.Count > 2)
			{
				for (int i = 0; i < list.Count; i++)
				{
					for (int j = i + 1; j < list.Count<string>(); j++)
					{
						if (list[i] == list[j])
						{
							list.Remove(list[j]);
							j--;
						}
					}
				}
			}
			return list;
		}

		public List<string> GetHVACRibbonList()
		{
			List<string> list = new List<string>();
			string strA = Path.Combine(Product.DataLocation, "HYHVACRibbon.xml");
			string strB = Path.Combine(Product.DataLocation, "HYHVACTagRibbon.xml");
			list.AddRange(this.GetTwoXmlNode(strA, strB));
			string strA2 = Path.Combine(Product.DataLocation, "HYGPSTagRibbon.xml");
			list.AddRange(this.GetXmlNode(strA2));
			string strA3 = Path.Combine(Product.DataLocation, "HYToolsRibbon.xml");
			string strB2 = Path.Combine(Product.DataLocation, "HYTagRibbon.xml");
			list.AddRange(this.GetTwoXmlNode(strA3, strB2));
			return list;
		}

		public List<string> GetDrainageList()
		{
			List<string> list = new List<string>();
			string strA = Path.Combine(Product.DataLocation, "HYGPSRibbon.xml");
			string strB = Path.Combine(Product.DataLocation, "HYGPSTagRibbon.xml");
			list.AddRange(this.GetTwoXmlNode(strA, strB));
			string strA2 = Path.Combine(Product.DataLocation, "HYToolsRibbon.xml");
			string strB2 = Path.Combine(Product.DataLocation, "HYTagRibbon.xml");
			list.AddRange(this.GetTwoXmlNode(strA2, strB2));
			return list;
		}

		public List<string> GetElecticRibbonList()
		{
			string strA = Path.Combine(Product.DataLocation, "HYElectricRibbon.xml");
			string strB = Path.Combine(Product.DataLocation, "HYToolsRibbon.xml");
			return this.GetTwoXmlNode(strA, strB);
		}

		public List<string> GetArchRibbonList()
		{
			string strA = Path.Combine(Product.DataLocation, "Ribbon.xml");
			return this.GetXmlNode(strA);
		}

		public List<string> GetTunnelRibbonList()
		{
			string strA = Path.Combine(Product.DataLocation, "Ribbon.xml");
			return this.GetXmlNode(strA);
		}

		public List<string> GetEDDRibbonList()
		{
			string strA = Path.Combine(Product.DataLocation, "HYEDD.xml");
			return this.GetXmlNode(strA);
		}

		public List<string> GetTwoXmlNode(string strA, string strB)
		{
			if (File.Exists(strA) && File.Exists(strB))
			{
				List<string> list = new List<string>();
				XmlDocument xmlDocument = new XmlDocument();
				XmlDocument xmlDocument2 = new XmlDocument();
				xmlDocument.Load(strA);
				xmlDocument2.Load(strB);
				XmlNode documentElement = xmlDocument.DocumentElement;
				XmlElement documentElement2 = xmlDocument2.DocumentElement;
				foreach (object obj in documentElement.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					list.Add(xmlNode.Attributes.Item(0).Value);
				}
				foreach (object obj2 in documentElement2.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj2;
					list.Add(xmlNode2.Attributes.Item(0).Value);
				}
				return list;
			}
			return null;
		}

		public List<string> GetXmlNode(string strA)
		{
			if (File.Exists(strA))
			{
				List<string> list = new List<string>();
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(strA);
				foreach (object obj in xmlDocument.DocumentElement.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					list.Add(xmlNode.Attributes.Item(0).Value);
				}
				return list;
			}
			return null;
		}

		public void ReadXmlAllMenu()
		{
			try
			{
				foreach (string text in this.GetXmlPaths())
				{
					if (File.Exists(text))
					{
						XmlDocument xmlDocument = new XmlDocument();
						xmlDocument.Load(text);
						foreach (object obj in xmlDocument.DocumentElement.ChildNodes)
						{
							XmlNode xmlNode = (XmlNode)obj;
							if (xmlNode.Attributes.Count == 4)
							{
								Shortcuts shortcuts = new Shortcuts();
								shortcuts.CommandName = xmlNode.Attributes.Item(0).Value;
								shortcuts.Shortcut = xmlNode.Attributes.Item(2).Value;
								shortcuts.Paths = xmlNode.Attributes.Item(3).Value;
								if (!this.listAll.Contains(shortcuts))
								{
									this.listAll.Add(shortcuts);
								}
							}
							else if (xmlNode.Attributes.Count == 3)
							{
								Shortcuts shortcuts2 = new Shortcuts();
								shortcuts2.CommandName = xmlNode.Attributes.Item(0).Value;
								shortcuts2.Shortcut = "";
								shortcuts2.Paths = xmlNode.Attributes.Item(2).Value;
								if (!this.listAll.Contains(shortcuts2))
								{
									this.listAll.Add(shortcuts2);
								}
							}
						}
					}
				}
				if (this.listAll.Count > 1)
				{
					for (int i = 0; i < this.listAll.Count<Shortcuts>(); i++)
					{
						for (int j = i + 1; j < this.listAll.Count<Shortcuts>(); j++)
						{
							if (this.listAll[i].CommandName == this.listAll[j].CommandName && this.listAll[i].Paths == this.listAll[j].Paths)
							{
								this.listAll.Remove(this.listAll[j]);
								j--;
							}
						}
					}
				}
			}
			catch (Exception)
			{
			}
		}

		public List<Shortcuts> childList(string str, List<Shortcuts> list)
		{
			List<Shortcuts> list2 = new List<Shortcuts>();
			foreach (Shortcuts shortcuts in list)
			{
				if (shortcuts.Paths.Contains(str.Trim() + ">"))
				{
					list2.Add(shortcuts);
				}
			}
			return list2;
		}

		public void InitTreeview()
		{
			try
			{
				TreeNode treeNode = new TreeNode();
				treeNode.Text = "盈建科菜单";
				this.treeView1.Nodes.Add(treeNode);
				if (this.GetAppMenuList().Count > 0)
				{
					foreach (string text in this.GetAppMenuList())
					{
						TreeNode treeNode2 = new TreeNode();
						treeNode2.Text = text.Trim();
						treeNode.Nodes.Add(treeNode2);
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		private void FormShortcutsSetting_Load(object sender, EventArgs e)
		{
			this.tbShortcut.Enabled = false;
			this.InitTreeview();
			this.ReadXmlAllMenu();
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.HeaderText = "菜单命令";
			dataGridViewTextBoxColumn.Width = 150;
			dataGridViewTextBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn.ReadOnly = true;
			this.dataGridView1.Columns.AddRange(new DataGridViewColumn[]
			{
				dataGridViewTextBoxColumn
			});
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn2.HeaderText = "快捷方式";
			dataGridViewTextBoxColumn2.Width = 100;
			dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn2.ReadOnly = false;
			this.dataGridView1.Columns.AddRange(new DataGridViewColumn[]
			{
				dataGridViewTextBoxColumn2
			});
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn3.HeaderText = "路径";
			dataGridViewTextBoxColumn3.Width = 180;
			dataGridViewTextBoxColumn3.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn3.ReadOnly = true;
			this.dataGridView1.Columns.AddRange(new DataGridViewColumn[]
			{
				dataGridViewTextBoxColumn3
			});
			this.dataGridView1.Rows.Clear();
			if (this.listAll.Count > 0)
			{
				foreach (Shortcuts shortcuts in this.listAll)
				{
					object[] values = new object[]
					{
						shortcuts.CommandName,
						shortcuts.Shortcut,
						shortcuts.Paths
					};
					this.dataGridView1.Rows.Add(values);
				}
				this.dataGridView1.Rows[0].Selected = false;
			}
			this.btDesignted.Enabled = false;
		}

		private void btDesignted_Click(object sender, EventArgs e)
		{
		}

		private void btSave_Click(object sender, EventArgs e)
		{
			this.SaveShortcutsXml();
			this.IsModify = false;
			base.Close();
		}

		private void SaveShortcutsXml()
		{
			this.WriteXml();
			YJKMessageBox.Information("保存成功，重启软件后快捷键生效");
		}

		private void WriteXml()
		{
			try
			{
				foreach (string text in this.GetXmlPaths())
				{
					if (File.Exists(text))
					{
						XmlDocument xmlDocument = new XmlDocument();
						xmlDocument.Load(text);
						XmlElement documentElement = xmlDocument.DocumentElement;
						foreach (Shortcuts shortcuts in this.listAll)
						{
							foreach (object obj in documentElement.ChildNodes)
							{
								XmlNode xmlNode = (XmlNode)obj;
								if (shortcuts.CommandName == xmlNode.Attributes.Item(0).Value)
								{
									if (xmlNode.Attributes.Count == 4 && shortcuts.Paths == xmlNode.Attributes.Item(3).Value)
									{
										xmlNode.Attributes.Item(2).Value = shortcuts.Shortcut;
										break;
									}
									if (xmlNode.Attributes.Count == 3 && shortcuts.Paths == xmlNode.Attributes.Item(2).Value)
									{
										xmlNode.Attributes.Item(2);
										xmlNode.Attributes.RemoveAt(2);
										XMLOperating.AddAttribute(xmlNode as XmlElement, "Shortcuts", shortcuts.Shortcut);
										XMLOperating.AddAttribute(xmlNode as XmlElement, "Paths", shortcuts.Paths);
										break;
									}
								}
							}
						}
						xmlDocument.Save(text);
					}
				}
				this.listAll.Clear();
			}
			catch (Exception)
			{
			}
		}

		private void btExist_Click(object sender, EventArgs e)
		{
			if (!this.IsModify)
			{
				base.Close();
				return;
			}
			DialogResult dialogResult = YJKMessageBox.Show("还未保存快捷键的修改，是否保存？", MessageBoxButtons.YesNo);
			if (dialogResult == DialogResult.Yes)
			{
				this.SaveShortcutsXml();
				base.Close();
				return;
			}
			if (dialogResult == DialogResult.No)
			{
				base.Close();
			}
		}

		private void dataGridView1_SelectionChanged(object sender, EventArgs e)
		{
		}

		private void tbShortcut_TextChanged(object sender, EventArgs e)
		{
			if (this.tbShortcut.Text.Trim() != "")
			{
				this.btDesignted.Enabled = true;
			}
		}

		private void cellbuttonClick(object sender, DataGridViewCellEventArgs e)
		{
			this.dataGridView1.BeginEdit(false);
		}

		private void AfterSelectNode(object sender, TreeViewEventArgs e)
		{
			try
			{
				this.treeView1.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
				this.treeView1.SelectedNode.ForeColor = System.Drawing.Color.White;
				this.dataGridView1.Rows.Clear();
				if (e.Node.Nodes == null || e.Node.Nodes.Count < 1)
				{
					List<Shortcuts> list = this.childList(e.Node.Text, this.listAll);
					if (list.Count > 0)
					{
						foreach (Shortcuts shortcuts in list)
						{
							object[] values = new object[]
							{
								shortcuts.CommandName,
								shortcuts.Shortcut,
								shortcuts.Paths
							};
							this.dataGridView1.Rows.Add(values);
						}
					}
					this.dataGridView1.Rows[0].Selected = false;
					this.tbShortcut.Enabled = false;
				}
				else
				{
					this.dataGridView1.Rows.Clear();
					if (this.listAll.Count > 0)
					{
						foreach (Shortcuts shortcuts2 in this.listAll)
						{
							object[] values2 = new object[]
							{
								shortcuts2.CommandName,
								shortcuts2.Shortcut,
								shortcuts2.Paths
							};
							this.dataGridView1.Rows.Add(values2);
						}
					}
					this.dataGridView1.Rows[0].Selected = false;
					this.tbShortcut.Enabled = false;
				}
			}
			catch (Exception)
			{
			}
		}

		private void treeview_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.treeView1.SelectedNode != null)
			{
				this.treeView1.SelectedNode.BackColor = System.Drawing.Color.White;
				this.treeView1.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void dataGriedView_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
		{
			e.Value = e.Value.ToString().ToUpper();
			e.ParsingApplied = true;
		}

		private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			bool flag = false;
			if (this.dataGridView1.CurrentCell.Value.ToString() != "")
			{
				foreach (Shortcuts shortcuts in this.listAll)
				{
					if (shortcuts.Shortcut == this.dataGridView1.CurrentCell.Value.ToString())
					{
						flag = true;
						DialogResult dialogResult = YJKMessageBox.Show("当前已将快捷键命令指定给 " + shortcuts.CommandName + "，\n如果允许重复请确定，否则取消", MessageBoxButtons.OKCancel);
						if (dialogResult == DialogResult.OK)
						{
							foreach (Shortcuts shortcuts2 in this.listAll)
							{
								if (shortcuts2.CommandName == this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString() && shortcuts2.Paths == this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString())
								{
									shortcuts2.Shortcut = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
									break;
								}
							}
							this.tbShortcut.Text = "";
							this.IsModify = true;
							break;
						}
						if (dialogResult == DialogResult.Cancel)
						{
							this.dataGridView1.CurrentCell.Value = "";
							this.IsModify = false;
							break;
						}
					}
					else
					{
						flag = false;
					}
				}
				if (!flag)
				{
					this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[1].Value = this.dataGridView1.CurrentCell.Value;
					foreach (Shortcuts shortcuts3 in this.listAll)
					{
						if (shortcuts3.CommandName == this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString() && shortcuts3.Paths == this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString())
						{
							shortcuts3.Shortcut = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
							break;
						}
					}
					this.tbShortcut.Text = "";
					this.IsModify = true;
					return;
				}
			}
			else if (this.dataGridView1.CurrentCell.Value.ToString() == "")
			{
				foreach (Shortcuts shortcuts4 in this.listAll)
				{
					if (shortcuts4.CommandName == this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString() && shortcuts4.Paths == this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString())
					{
						shortcuts4.Shortcut = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
						break;
					}
				}
				return;
			}
		}

		private bool IsModify;

		private int m_DisciplineValue = Product.GetDisciplineValue();

		public List<Shortcuts> listAll = new List<Shortcuts>();
	}
}
